package com.weishi.project.training.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.project.training.domain.SchemeDetails;
import com.weishi.project.training.domain.TrainingDay;
import com.weishi.project.training.service.ISchemeDetailsService;
import com.weishi.project.training.service.ITrainingDayService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import com.weishi.project.training.domain.Scheme;
import com.weishi.project.training.service.ISchemeService;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.web.page.TableDataInfo;

/**
 * 训练方案 主表
 */
@RestController
@RequestMapping("/training/scheme")
public class SchemeController extends BaseController {

    /**
     * 训练方案主表接口
     */
    @Autowired
    private ISchemeService schemeService;

    /**
     * 训练方案子表接口
     */
    @Autowired
    private ISchemeDetailsService schemeDetailsService;

    /**
     * 训练课程接口
     */
    @Autowired
    private ITrainingDayService trainingDayService;



    @ApiOperation("查询训练方案列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public TableDataInfo list(Scheme scheme) {
        startPage();
        List<Scheme> list = schemeService.selectSchemeList(scheme);
        return getDataTable(list);
    }


    @ApiOperation("导出训练方案列表")
    @Log(title = "训练方案", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Scheme scheme) {
        List<Scheme> list = schemeService.selectSchemeList(scheme);
        ExcelUtil<Scheme> util = new ExcelUtil<Scheme>(Scheme.class);
        return util.exportExcel(list, "scheme");
    }


    @ApiOperation("获取训练方案详细信息")
    @RequestMapping(value = "/schemeById", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult schemeById(Long id) {
        return schemeService.schemeById(id);
    }


    @ApiOperation("新增训练方案 和 组")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult add(@RequestBody Scheme scheme) {
        return schemeService.add(scheme);
    }


    @ApiOperation("修改训练方案 和 组")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult edit(@RequestBody Scheme scheme) {
        return schemeService.edit(scheme);
    }


    @ApiOperation("删除训练方案 和 组")
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deleteById(Long id) {
        // 判断这个训练方案有没有课程
        Scheme scheme = schemeService.getById(id);
        List<TrainingDay> trainingDay = trainingDayService.list(Wrappers.<TrainingDay>lambdaQuery().eq(TrainingDay::getSchemeId , scheme.getId()));
        if(trainingDay.size() > 0){
            // 前端判断 data = 1 就提示 无法删除方案，请先删除训练课！
            return AjaxResult.success("无法删除方案，请先删除训练课！" , 1);
        }

        // 删除训练方案
        schemeService.removeById(id);

        // 删除训练方案组
        List<SchemeDetails> list = schemeDetailsService.list(Wrappers.<SchemeDetails>lambdaQuery().eq(SchemeDetails::getTblSchemeId , id));
        if(list.size() > 0){
            list.forEach( p-> {
                schemeDetailsService.removeById(p.getId());
            });
        }

        // 代码到这里提示删除成功！
        return AjaxResult.success();
    }






}